package com.lenovo.smarttraffic.zm;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

public class Network_setActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_ip1;
    private EditText et_ip2;
    private Button ipSet;
    private Boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        initToolBar(findViewById(R.id.toolbar), true, "ip设置");
        et_ip1 = (EditText) findViewById(R.id.et_ip1);
        et_ip2 = (EditText) findViewById(R.id.et_ip2);
//        et_ip1.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//                CheckIp1();
//            }
//        });
//        et_ip2.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//                CheckIp2();
//            }
//        });
        ipSet = (Button) findViewById(R.id.ipSet);
        ipSet.setOnClickListener(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_network_set;
    }


    private void CheckIp1() {
        String ip1 = et_ip1.getText().toString().trim();
        if (ip1.equals("")) {
            Toast.makeText(this, "你输入的有误，请重新输入", Toast.LENGTH_SHORT).show();
            flag = false;
            return;
        } else if (ip1.contains("x")) {
            Toast.makeText(this, "你输入的有误，请重新输入", Toast.LENGTH_SHORT).show();
            et_ip1.setText(null);
            flag = false;
            return;
        } else {
            Integer integer2 = Integer.valueOf(ip1);
            if (integer2 > 255 || integer2 < 0) {
                Toast.makeText(this, "你输入的有误，请重新输入", Toast.LENGTH_SHORT).show();
                et_ip1.setText(null);
                flag = false;
                return;
            }
        }

        flag = true;

    }

    private void CheckIp2() {
        String ip2 = et_ip2.getText().toString().trim();
        if (ip2.equals("")) {
            Toast.makeText(this, "你输入的有误，请重新输入", Toast.LENGTH_SHORT).show();
            flag = false;
            return;
        } else if (ip2.contains("x")) {
            Toast.makeText(this, "你输入的有误，请重新输入", Toast.LENGTH_SHORT).show();
            et_ip2.setText(null);
            flag = false;
            return;
        } else {
            Integer integer2 = Integer.valueOf(ip2);
            if (integer2 > 255 || integer2 < 0) {
                Toast.makeText(this, "你输入的有误，请重新输入", Toast.LENGTH_SHORT).show();
                et_ip2.setText(null);
                flag = false;
                return;
            }

        }
        flag = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ipSet:
                CheckIp1();

                //避免两次  提前结束
                if (flag) {
                    CheckIp2();
                }
                if (flag) {
                    finish();
                }
                break;
        }
    }
}
